package Datebase;

import redis.clients.jedis.Jedis;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.URL;
import java.sql.*;

public class tmodify extends JFrame {
    public static void main(String[] args) {
        System.setProperty("sun.java2d.noddraw", "true");
        new tmodify("tno");
    }
    public tmodify(String tno){
        JFrame mod = new JFrame("修改密码");
        URL resource = tmodify.class.getClassLoader().getResource("kun.png");
        Image image = new ImageIcon(resource).getImage();
        mod.setIconImage(image);
        //窗口尺寸
        mod.setSize(400,200);
        mod.setLocationRelativeTo(null);


        mod.setLayout(new GridLayout(5,3));
        //
        JPanel panel1 = new JPanel();
        JLabel label1 = new JLabel("旧密码：");
        panel1.add(label1);
        JPasswordField textField1 = new JPasswordField();
        textField1.setColumns(10);
        panel1.add(textField1);
        mod.add(panel1);

        JPanel panel2 = new JPanel();
        JLabel label2 = new JLabel("新密码：");
        panel2.add(label2);
        JPasswordField textField2 = new JPasswordField();
        textField2.setColumns(10);
        panel2.add(textField2);
        mod.add(panel2);
        mod.setVisible(true);

        JButton button3 = new JButton("确认");
        JButton button4 = new JButton("返回");
        JPanel panel4 = new JPanel();
        panel4.add(button3);
        panel4.add(button4);
        mod.add(panel4);
        button4.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                new datab();
            }
        });

        button3.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (e.getSource() == button3) {

                    try {
                        Class.forName("oracle.jdbc.driver.OracleDriver");
                    } catch (ClassNotFoundException ex) {
                        throw new RuntimeException(ex);
                    }

                    Connection connection = null;
                    try {
                        connection = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE" ,"HR","root");
                    } catch (SQLException ex) {
                        throw new RuntimeException(ex);
                    }

                    try {
                        String uid = tno;// 从文本框获取学号
                        Jedis jedis = new Jedis("127.0.0.1",6379);// 建立redis连接
                        String password = jedis.get(uid);

                            String pw1 = "";
                            String un = "";
                            Statement statement = connection.createStatement();

                            ResultSet resultSet = statement.executeQuery("select TCODE From TEACHER where TCODE='" + textField1.getText() +"'");

                            while (resultSet.next()) {
                                un = resultSet.getString(1);

                            }



                            if (!textField1.getText().equals(un) || textField1.getText().equals("")) {
                                JOptionPane.showMessageDialog(null, "旧密码不正确！",
                                        "提示", JOptionPane.WARNING_MESSAGE);
                            }
                            else {
                                System.out.println(textField2.getText());
                                JOptionPane.showMessageDialog(null, "修改成功！",
                                        "提示", JOptionPane.INFORMATION_MESSAGE);
                                ResultSet resultSet1 = statement.executeQuery("UPDATE TEACHER SET TCODE = '" +textField2.getText()+"' WHERE TNO = '"+ tno +"'");
                                while (resultSet.next()) {
                                    pw1 = resultSet1.getString(1);

                                }
                                System.out.println(textField2.getText());
                                jedis.set(tno,textField2.getText());
                            }

                            connection.close();


                    } catch (SQLException ex) {
                        System.out.println(ex);
                    }

                }
            }
        });

    }
}
